/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.foxline.manager.search;

import br.com.foxline.enums.Status;
import br.com.foxline.model.Enterprise;
import br.com.foxline.model.Lot;
import br.com.foxline.service.EnterpriseService;
import br.com.foxline.service.LotService;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.model.SelectItem;

/**
 *
 * @author roniere
 */
@ManagedBean
@ViewScoped
public class ManagerSituationEnterprise implements Serializable {

    @EJB
    private LotService lotService;
    @EJB
    private EnterpriseService enterpriseService;
    private Enterprise enterprise;
    private Integer block;
    private Lot lot;
    private List<Lot> lots;
    private Status status;

    @PostConstruct
    public void init() {

        this.enterprise = new Enterprise();
        this.lot = new Lot();
        this.lots = new ArrayList<Lot>();

    }

    public Enterprise getEnterprise() {
        return enterprise;
    }

    public void setEnterprise(Enterprise enterprise) {
        this.enterprise = enterprise;
    }

    public List<Lot> getLots() {
        return lots;
    }

    public void setLots(List<Lot> lots) {
        this.lots = lots;
    }

    public Integer getBlock() {
        return block;
    }

    public void setBlock(Integer block) {
        this.block = block;
    }

    public Lot getLot() {
        return lot;
    }

    public void setLot(Lot lot) {
        this.lot = lot;
    }

    public List<Enterprise> autocompleteEnterprise(String query) {
        return enterpriseService.autocomplete(query);
    }

    public List<SelectItem> lotsByEnterpriseAndBlock() {
        List<SelectItem> items = new ArrayList<SelectItem>();
        List<Lot> lots = lotService.lotsByEnterprise(enterprise, block);
        for (Lot lot : lots) {
            items.add(new SelectItem(lot.getId(), "Lote " + lot.getNum().toString()));
        }
        return items;
    }

    public List<SelectItem> selectTypeBlock() {
        List<SelectItem> items = new ArrayList<SelectItem>();
        if (enterprise != null && enterprise.getQtdBlock() != null) {
            for (int i = 1; i <= enterprise.getQtdBlock(); i++) {
                items.add(new SelectItem(i, "Quadra " + i));
            }
        }
        return items;
    }

  
    public List<SelectItem> selectStatus() {
        List<SelectItem> items = new ArrayList<SelectItem>();
        for (Status status:Status.values()) {
            items.add(new SelectItem(status, "" + status.getName()));
        }
        return items;
    }
    
    

    public void search() {
        lots = enterpriseService.findEnterprise(enterprise, lot, block, status);
    }

    public Status getStatus() {
        return status;
    }

    public void setStatus(Status status) {
        this.status = status;
    }
}
